package com._520it.util;

import java.sql.Statement;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

public class JdbcUtil {

	private static DataSource dataSource;
	static {
		// 获取配置文件
		ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
		InputStream inputStream = clsLoader.getResourceAsStream("db.properties");
		Properties p = new Properties();
		try {
			p.load(inputStream);
			dataSource = DruidDataSourceFactory.createDataSource(p);
		} catch (Exception e) {
			throw new RuntimeException("", e);
		}
	}

	public static Connection getConn() {
		try {
			return dataSource.getConnection();
		} catch (Exception e) {
			throw new RuntimeException("", e);
		}
	}

	public static void close(Connection coon, Statement st, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (coon != null) {
						coon.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
